package com.ylsd.dynamic.controller;

import com.ylsd.dynamic.entity.DynamicCatalogin;
import com.ylsd.dynamic.entity.DynamicList;
import com.ylsd.dynamic.entity.School;
import com.ylsd.dynamic.service.DynamicService;
import com.ylsd.dynamic.util.QiniuUtil;
import org.springframework.web.bind.annotation.*;
import org.ylsdxy.model.response.CustomPage;
import org.ylsdxy.model.response.ResponseResult;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/dynamic")
public class DynamicController {

    @Resource
    private DynamicService dynamicService;
    @Resource
    QiniuUtil qiniuUtil;

    @PostMapping("/findDCByPage")
    public ResponseResult<CustomPage<List<DynamicCatalogin>>> findDCByPage(@RequestBody DynamicCatalogin dynamicCatalogin){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",dynamicService.findByPage(dynamicCatalogin));
    }

    @PostMapping("/insertDC")
    public ResponseResult<Boolean> insertDC(@RequestBody DynamicCatalogin dynamicCatalogin){
        boolean result = dynamicService.insertDC(dynamicCatalogin);
        if (result){
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else {
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"error",null);
        }
    }

    @DeleteMapping("/delDC/{id}")
    public ResponseResult<Boolean> delDC(@PathVariable List<Integer> id){
        boolean result = dynamicService.delDC(id);
        if (result){
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else {
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"error",null);
        }
    }

    @PostMapping("/updatetDC")
    public ResponseResult<Boolean> updatetDC(@RequestBody DynamicCatalogin dynamicCatalogin){
        boolean result = dynamicService.updatetDC(dynamicCatalogin);
        if (result){
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else {
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"error",null);
        }
    }

    @PostMapping("/findDLByPage")
    public ResponseResult<CustomPage<List<DynamicList>>> findDLByPage(@RequestBody DynamicList dynamicList){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",dynamicService.findDLByPage(dynamicList));
    }

    @PostMapping("/insertDL")
    public ResponseResult<Boolean> insertDL(@RequestBody DynamicList dynamicList){
        boolean result = dynamicService.insertDL(dynamicList);
        if (result){
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else {
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"error",null);
        }
    }

    @PostMapping("/updateDL")
    public ResponseResult<Boolean> updateDL(@RequestBody DynamicList dynamicList){
        boolean result = dynamicService.updateDL(dynamicList);
        if (result){
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else {
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"error",null);
        }
    }

    @PostMapping("/delDL")
    public ResponseResult<Boolean> delDL(@RequestBody List<Integer> id){
        boolean result = dynamicService.delDL(id);
        if (result){
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else {
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"error",null);
        }
    }

    @GetMapping("/choiceSchool")
    public ResponseResult<List<School>> choiceSchool(){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",dynamicService.choiceSchool());
    }

    @GetMapping("/choiceDynamic")
    public ResponseResult<List<DynamicCatalogin>> choiceDynamic(){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",dynamicService.choiceDynamic());
    }

    @PostMapping("/search")
    public ResponseResult<List<DynamicCatalogin>> search(@PathVariable Integer id){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",dynamicService.search(id));
    }

    @GetMapping("/getUpdateToken")
    public Object getUpdateToken(){
        String uploadToken = qiniuUtil.getUploadToken();
        System.out.println(uploadToken);
        ResponseResult responseResult = new ResponseResult(200,"",uploadToken);
        return responseResult;
    }

    @PostMapping("/findImgById")
    public ResponseResult<String> findImgById(){
        return new ResponseResult<>(200,"success",dynamicService.findImgById());
    }
}
